var pomelo = require('pomelo');
var routeUtil = require('./app/util/routeUtil');

/**
 * Init app for client.
 * 初始化应用程序客户机
 */
var app = pomelo.createApp();

/**
 * 设置服务器名称
 * */
app.set('name', 'chess_pomelo_ws');

/** app configuration
 * connetor 服务器配置
 * */
var routeFilter = require('./app/servers/connector/filter/routeFilter.js');
app.configure('production|development', 'connector', function () {
    app.set('connectorConfig',
        {
            connector: pomelo.connectors.hybridconnector,
            heartbeat: 3,
            useDict: true,
            useProtobuf: true
        });
    // app.filter(routeFilter());
});

/**
 * gate服务器配置
 * */
app.configure('production|development', 'gate', function () {
    app.set('connectorConfig',
        {
            connector: pomelo.connectors.hybridconnector,
            useProtobuf: true
        });
});

/**
 * psz服务器配置
 * */
app.configure('production|development', function () {
    // route configures
    // app.route("psz", routeUtil.psz);

    // filter configures
    app.filter(pomelo.timeout());
});

/**
 * tenThirty服务器配置
 * */
app.configure('production|development', function () {
    // route configures
    // app.route('tenThirty', routeUtil.tenThirty);

    // filter configures
    app.filter(pomelo.timeout());
});

/**
 * chat服务器配置
 * */
app.configure('production|development', function () {
    // route configures
    app.route('chat', routeUtil.chat);

    // filter configures
    app.filter(pomelo.timeout());
});

/**
 * chat过滤器
 * */
var abuseFilter = require('./app/servers/chat/filter/abuseFilter.js');
app.configure('production|development', 'chat', function () {
    app.filter(abuseFilter());
});

/**
 * 启动应用程序
 * */
app.start();

process.on('uncaughtException', function (err) {
    console.error(' Caught exception: ' + err.stack);
});

/**
 * 加载app生命周期组件
 * */
var applifecycle = require('./app/components/applifecycle');
app.configure('production|development', 'master', function () {
    app.load(applifecycle, {interval: 5000});
});

/**
 * 删除指定的值
 * */
Array.prototype.remove = function (value) {
    if (typeof value === 'undefined') {
        return false;
    }
    let index = this.indexOf(value);
    if (index > -1) {
        this.splice(index, 1);
    }
    return true;
};